//
//  DetailsCollectionViewCell.m
//  CarOfHome2
//
//  Created by 微 醺 on 15/9/24.
//  Copyright (c) 2015年 Frank. All rights reserved.
//

#import "DetailsCollectionViewCell.h"
#import "TextTableViewCell.h"//文字
#import "PartsTableViewCell.h"//零件
#import "DesignerTableViewCell.h"//作者
#import "CellModel.h"
#import "CollectionModel.h"
#import "ItemModel.h"
#import "DoubleDetailsViewController.h"//配件VC
#import "DesignerViewController.h"//作者VC

@implementation DetailsCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) style:UITableViewStylePlain];
        self.tableView.dataSource = self;
        self.tableView.delegate = self;
        [self.contentView addSubview:self.tableView];
        
        self.count++;
    }
    return self;
}


-(void)setDic:(NSDictionary *)dic
{
    _dic = dic;
    
    [self dataHandle];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width,  300 * SAHeight )];
    self.image = [[UIImageView alloc]init];
    self.image.frame = CGRectMake(0, 0, self.frame.size.width,  300 * SAHeight );
    NSString *str = dic[@"p_link"];
    [self.image sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:nil];
    [view addSubview:self.image];
    self.tableView.tableHeaderView = view;
    
    
    
    
    UIView *emptyLine = [[UIView alloc] init];
    emptyLine.backgroundColor = [UIColor clearColor];
    self.tableView.tableFooterView = emptyLine;
    
    [self.tableView registerClass:[TextTableViewCell class] forCellReuseIdentifier:@"reuse1"];
    [self.tableView registerClass:[DesignerTableViewCell class] forCellReuseIdentifier:@"reuse2"];
    [self.tableView registerClass:[PartsTableViewCell class] forCellReuseIdentifier:@"reuse3"];
    
    [self.tableView reloadData];
}


- (void)dataHandle
{
    NSMutableString *url = [NSMutableString stringWithFormat:@"%s%s",Hot_rod1,Hot_rod2];
    self.count = self.index / 20 + 1;
    NSString *str = [NSString stringWithFormat:@"%ld", self.count];
    [url appendString:str];
    if (self.index >= 20) {
        self.index = self.index - (self.count - 1) * 20;
    }
    self.priceArr = [NSMutableArray array];
    self.arr = [NSMutableArray array];
    [HTTPTool get:url body:nil httpResult:JSON success:^(id result) {
        NSMutableArray *array = result[@"cases"];
        NSDictionary *dic = [array objectAtIndex:self.index];
        CollectionModel *model = [[CollectionModel alloc] init];
        [model setValuesForKeysWithDictionary:dic];
        
        model.person = [[CellModel alloc] init];
        
        NSDictionary *personDic = [dic objectForKey:@"designer"];
        [model.person setValuesForKeysWithDictionary:personDic];
        
        [self.priceArr addObject:model];
        NSArray *itemArray = [self.dic objectForKey:@"items"];
        self.itemArr = [NSMutableArray array];
        for (NSDictionary *itemDic in itemArray) {
            ItemModel *itemModel = [[ItemModel alloc] init];
            [itemModel setValuesForKeysWithDictionary:itemDic];
            [self.itemArr addObject:itemModel];
            }
            [self.tableView reloadData];
        } failure:^(NSError *error) {
            NSLog(@"%@", error);
            
    }];
}


#pragma mark - scrollView 图片下拉放大
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat yy = self.tableView.contentOffset.y;
    if (yy <= 0)
    {
        yy = 0 - yy;
        self.image.frame = CGRectMake(-yy, -yy, self.frame.size.width + yy + yy, SAWindth * 300 + yy);
            
    }
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSString *str = [NSString stringWithFormat:@"%@", self.dic[@"position"]];
    if ([str isEqualToString:@"1"]) {
        return self.priceArr.count + 1;
    }
    return 1 + self.itemArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    
    
    
    
    if (indexPath.row == 0)
    {
        TextTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse1"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.text.text = self.dic[@"description"];
        return cell;
    }
    else
    {
        
        NSString *str = [NSString stringWithFormat:@"%@", self.dic[@"position"]];
        if ([str isEqualToString:@"1"] && indexPath.row == 1)
        {
            DesignerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse2"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.model = [self.priceArr objectAtIndex:0];
            
            return cell;
        }
        else
        {
            PartsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse3"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.itemModel = [self.itemArr objectAtIndex:indexPath.row - 1];
            return  cell;
        }
        

    }


}

//找到VC 用于push下一个VC
- (UIViewController *)viewController
{
    for (UIView *next = [self superview]; next; next = next.superview)
    {
        UIResponder *nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]])
        {
            return (UIViewController *)nextResponder;
        }
    }
    return nil;
}

/* 选中方法 */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *str = [NSString stringWithFormat:@"%@", self.dic[@"position"]];
    if (indexPath.row != 0 && indexPath.row == 1 && [str isEqualToString:@"1"])
    {
        DesignerViewController *designer = [[DesignerViewController alloc] init];
        CollectionModel *model = [self.priceArr objectAtIndex:0];
        designer.model = model;
        [[self viewController].navigationController pushViewController:designer animated:YES];

    }
    else
    {
        if (indexPath.row != 0)
        {
            
            DoubleDetailsViewController *DD = [[DoubleDetailsViewController alloc] init];
            
            ItemModel *model = [self.itemArr objectAtIndex:indexPath.row - 1];
            DD.model = model;
            
            [[self viewController].navigationController pushViewController:DD animated:YES];
        }
        
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0)
    {
        
        return [TextTableViewCell heightWithText:self.dic[@"description"]];
    }
    else
        return 120;
}




@end
